package com.day1020;
/**
 * 英雄机
 */

import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.util.Timer;
import java.util.TimerTask;

public class Hero extends FlySprite implements ActionSprite {
    private int life;//  生命值
    private int doubleFile;//   火力值
    private int score = 0;//   得分

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }

    public int getLife() {
        return life;
    }
    //  添加血量
    public void addLife() {
        life ++;
    }
    //  减少血量
    public void subtractLife(){
        life --;
    }

    public void addDoubleFile(){
        doubleFile += 40;
    }
    //  火力值清零
    public void setDoubleFile(int doubleFile) {
        this.doubleFile = doubleFile;
    }

    public Hero() {
        life = 3;
        init();
        Timer timer = new Timer();
        timer.schedule(new HeroAnimation(),0,150);
    }

    //  声明标记
    int flag = 1;
    int num = 0;

    class HeroAnimation extends TimerTask{
        @Override
        public void run() {
            if (isDead()){
                num ++;
                Hero.this.image = ImageLoadUtils.load("hero_blowup_n" + num + ".png");
            }
            if (num == 4){
                setDead(false);
                num = 0;
            }else {
                if (flag == 1){
                    flag = 2;
                }else {
                    flag = 1;
                }
                Hero.this.image = ImageLoadUtils.load("hero" + flag + ".png");
            }
        }
    }

    @Override
    public void init() {
        BufferedImage img = ImageLoadUtils.load("hero1.png");
        super.image = img;
        //  缩小飞机为原来的一半
        super.width = super.image.getWidth() / 2;
        super.height = super.image.getHeight() / 2;
        //  起始位置
        super.x = (FlyFrame.WIDTH - this.width) / 2;
        super.y = (FlyFrame.HEIGHT - this.height - 50);
    }

    @Override
    public void destroy() {
        FlyPanel.gameState = GameState.GAME_OVER;
    }

    @Override
    public void move(int keyCode) {
        switch (keyCode){
            case KeyEvent.VK_UP:// 上
                int y = this.y - 10;
                if (y < 0){
                    y = 0;
                }
                this.y = y;
//                if (this.y > 0){
//                    this.y -= 10;
//                }
                break;
            case KeyEvent.VK_DOWN://  下
                y = this.y + 5;
                if (y > (600 - this.height - 45)){
                    y = 600 - this.height - 45;
                }
                this.y = y;
//                if (this.y < (600 - this.height - 45)){
//                    this.y += 10;
//                }
                break;
            case KeyEvent.VK_LEFT://  左
                int x = this.x - 10;
                if (x < 0){
                    x = 0;
                }
                this.x = x;
//                if (this.x > 0){
//                    this.x -= 10;
//                }
                break;
            case KeyEvent.VK_RIGHT:  //  右
                x = this.x + 10;
                if (x > (400 - this.width - 16)){
                    x = 400 - this.width - 16;
                }
                this.x = x;
//                if (this.x < (400 - this.width - 16)){
//                    this.x += 10;
//                }
                break;
        }
    }

    //  发射子弹
    public void shoot(){
        int xStep = this.width / 4;
        if (doubleFile > 0){ // 双发
            Bullet bullet1 = new Bullet(this.x + xStep * 1,this.y - 20);
            Bullet bullet2 = new Bullet(this.x + xStep * 3,this.y - 20);
            doubleFile -= 2;//   发射双倍火力，每次减2，实际就是2倍火力的持续时间
            FlyPanel.bulletList.add(bullet1);
            FlyPanel.bulletList.add(bullet2);
        }else {
            int x = this.x + xStep * 2;
            int y = this.y - 20;
            Bullet bullet = new Bullet(x,y);
            FlyPanel.bulletList.add(bullet);
        }

    }
}
